package com.cyc.gulimall.member.service.impl;

import com.cyc.gulimall.member.exception.PhoneException;
import com.cyc.gulimall.member.exception.UsernameException;
import com.cyc.gulimall.member.vo.MemberLoginVo;
import com.cyc.gulimall.member.vo.MemberUserRegisterVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyc.common.utils.PageUtils;
import com.cyc.common.utils.Query;

import com.cyc.gulimall.member.dao.MemberDao;
import com.cyc.gulimall.member.entity.MemberEntity;
import com.cyc.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberUserRegisterVo vo) {
        MemberEntity entity=new MemberEntity();
        //会员默认等级
        entity.setLevelId(1L);

        //检查手机号和用户名是否唯一
        checkPhoneUnique(vo.getPhone());
        checkUsernameUnique(vo.getUserName());

        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());
        //密码需要加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);

        this.baseMapper.insert(entity);

    }

    @Override
    public void checkPhoneUnique(String phone)throws PhoneException {
        int i = this.count(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (i>0){
            throw new PhoneException();
        }

    }

    @Override
    public void checkUsernameUnique(String username)throws UsernameException {
        int i = this.count(new QueryWrapper<MemberEntity>().eq("username", username));

        if (i>0){
            throw new UsernameException();
        }

    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
        if (entity==null){

            return null;
        }else {
            String entityPassword = entity.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
            boolean b = bCryptPasswordEncoder.matches(password, entityPassword);
            if (b!=true){
                return null;
            }else {
                return entity;
            }
        }


    }

}